home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_lrm / chap06.doc < prev    next >
Text File  |  1996-01-30  |  35KB  |  809 lines

  1.                               6. Subprograms
  2.  
  3.  
  4. Subprograms  are  one  of the four forms of program unit, of which programs
  5. can be composed.  The other forms are packages,  task  units,  and  generic
  6. units.
  7.  
  8.  
  9. A  subprogram  is a program unit whose execution is invoked by a subprogram
  10. call.  There are two forms of  subprogram:  procedures  and  functions.   A
  11. procedure  call  is  a  statement;   a  function  call is an expression and
  12. returns a value.  The definition of a subprogram can be given in two parts:
  13. a subprogram declaration defining its calling conventions, and a subprogram
  14. body defining its execution.
  15.  
  16. References:  function 6.5, function call 6.4, generic unit 12,  package  7,
  17. procedure  6.1,  procedure  call  6.4, subprogram body 6.3, subprogram call
  18. 6.4, subprogram declaration 6.1, task unit 9
  19.  
  20. 6.1  Subprogram Declarations
  21.  
  22.  
  23. A subprogram declaration declares a procedure or a function,  as  indicated
  24. by the initial reserved word.
  25.  
  26.  
  27.     subprogram_declaration ::= subprogram_specification;
  28.  
  29.     subprogram_specification ::=
  30.          procedure identifier [formal_part]
  31.        | function designator  [formal_part] return type_mark
  32.  
  33.     designator ::= identifier | operator_symbol
  34.  
  35.     operator_symbol ::= string_literal
  36.  
  37.     formal_part ::=
  38.        (parameter_specification {; parameter_specification})
  39.  
  40.     parameter_specification ::=
  41.        identifier_list : mode type_mark [:= expression]
  42.  
  43.     mode ::= [in] | in out | out
  44.  
  45.  
  46. The  specification  of  a procedure specifies its identifier and its formal
  47. parameters (if  any).   The  specification  of  a  function  specifies  its
  48. designator,  its formal parameters (if any) and the subtype of the returned
  49. value (the result subtype).  A designator that is  an  operator  symbol  is
  50. used  for  the  overloading  of  an  operator.   The sequence of characters
  51. represented by an operator symbol must be an operator belonging to  one  of
  52. the  six  classes  of  overloadable operators defined in section 4.5 (extra
  53. spaces are not allowed and the case of letters is not significant).
  54.  
  55.  
  56. A parameter specification with  several  identifiers  is  equivalent  to  a
  57. sequence  of  single parameter specifications, as explained in section 3.2.
  58. Each single parameter specification declares a  formal  parameter.   If  no
  59. mode  is  explicitly  given,  the  mode  in  is  assumed.   If  a parameter
  60. specification ends with  an  expression,  the  expression  is  the  default
  61. expression  of  the formal parameter.  A default expression is only allowed
  62. in a parameter specification if the  mode  is  in  (whether  this  mode  is
  63. indicated explicitly or implicitly).  The type of a default expression must
  64. be that of the corresponding formal parameter.
  65.  
  66.  
  67. The use of a name that denotes a formal parameter is not allowed in default
  68. expressions  of  a  formal  part  if  the specification of the parameter is
  69. itself given in this formal part.
  70.  
  71.  
  72. The elaboration of a subprogram declaration  elaborates  the  corresponding
  73. formal part.  The elaboration of a formal part has no other effect.
  74.  
  75.  
  76. Examples of subprogram declarations:
  77.  
  78.     procedure TRAVERSE_TREE;
  79.     procedure INCREMENT(X : in out INTEGER);
  80.     procedure RIGHT_INDENT(MARGIN : out LINE_SIZE);          --  see 3.5.4
  81.     procedure SWITCH(FROM, TO : in out LINK);                --  see 3.8.1
  82.  
  83.     function RANDOM return PROBABILITY;                      --  see 3.5.7
  84.  
  85.     function MIN_CELL(X : LINK) return CELL;                 --  see 3.8.1
  86.     function NEXT_FRAME(K : POSITIVE) return FRAME;          --  see 3.8
  87.     function DOT_PRODUCT(LEFT,RIGHT: VECTOR) return REAL;    --  see 3.6
  88.  
  89.     function "*"(LEFT,RIGHT : MATRIX) return MATRIX;         --  see 3.6
  90.  
  91.  
  92. Examples of in parameters with default expressions:
  93.  
  94.     procedure PRINT_HEADER(PAGES  : in NATURAL;
  95.                            HEADER : in LINE    :=  (1 .. LINE'LAST => ' ');  --  see 3.6
  96.                            CENTER : in BOOLEAN := TRUE);
  97.  
  98. Notes:
  99.  
  100.  
  101. The  evaluation  of  default  expressions  is  caused by certain subprogram
  102. calls, as described in section 6.4.2 (default expressions are not evaluated
  103. during the elaboration of the subprogram declaration).
  104.  
  105.  
  106. All subprograms can be called recursively and are reentrant.
  107.  
  108.  
  109. References:  declaration 3.1, elaboration 3.9, evaluation  4.5,  expression
  110. 4.4,  formal  parameter  6.2, function 6.5, identifier 2.3, identifier list
  111. 3.2, mode 6.2, name 4.1, elaboration has no other effect 3.9, operator 4.5,
  112. overloading 6.6 8.7, procedure 6, string literal 2.6, subprogram call  6.4,
  113. type mark 3.3.2
  114.  
  115. 6.2  Formal Parameter Modes
  116.  
  117.  
  118. The value of an object is said to be read when this value is evaluated;  it
  119. is  also  said to be read when one of its subcomponents is read.  The value
  120. of a variable is said to be updated when an assignment is performed to  the
  121. variable,  and  also  (indirectly)  when  the  variable  is  used as actual
  122. parameter of a subprogram call or entry call  statement  that  updates  its
  123. value;   it  is  also  said  to be updated when one of its subcomponents is
  124. updated.
  125.  
  126.  
  127. A formal parameter of a subprogram has one of the three following modes:
  128.  
  129.  
  130.   in      The formal parameter  is a  constant  and  permits   only reading  of the value  of the
  131.           associated actual parameter.
  132.  
  133.  
  134.   in out  The formal parameter is a variable  and permits both reading and updating of the  value
  135.           of the associated actual parameter.
  136.  
  137.  
  138.   out     The formal parameter is a variable  and permits updating of the value of the associated
  139.           actual parameter.
  140.  
  141.           The value  of  a scalar parameter that is not updated by the call is undefined  upon return;
  142.           the same  holds for the  value  of a  scalar subcomponent,  other  than a  discriminant.
  143.           Reading the  bounds and discriminants of the  formal parameter  and  of its subcomponents
  144.           is allowed,  but no other reading.
  145.  
  146.  
  147. For a scalar parameter, the above effects are achieved  by  copy:   at  the
  148. start  of  each  call, if the mode is in or in out, the value of the actual
  149. parameter is copied into  the  associated  formal  parameter;   then  after
  150. normal completion of the subprogram body, if the mode is in out or out, the
  151. value  of  the  formal  parameter is copied back into the associated actual
  152. parameter.  For a parameter whose type is an access type, copy-in  is  used
  153. for all three modes, and copy-back for the modes in out and out.
  154.  
  155.  
  156. For  a  parameter  whose  type  is  an  array,  record,  or  task  type, an
  157. implementation may likewise achieve the  above  effects  by  copy,  as  for
  158. scalar  types.   In  addition, if copy is used for a parameter of mode out,
  159. then copy-in is required at least for the bounds and discriminants  of  the
  160. actual  parameter  and of its subcomponents, and also for each subcomponent
  161. whose type is an access type.  Alternatively, an implementation may achieve
  162. these effects by reference, that is, by arranging that  every  use  of  the
  163. formal  parameter  (to  read or to update its value) be treated as a use of
  164. the associated actual parameter, throughout the execution of the subprogram
  165. call.  The language does not define which of these two mechanisms is to  be
  166. adopted  for  parameter  passing,  nor  whether different calls to the same
  167. subprogram are to use the same mechanism.  The execution of  a  program  is
  168. erroneous  if  its  effect  depends  on  which mechanism is selected by the
  169. implementation.
  170.  
  171.  
  172. For a parameter whose type  is  a  private  type,  the  above  effects  are
  173. achieved  according to the rule that applies to the corresponding full type
  174. declaration.
  175.  
  176.  
  177. Within the body of a subprogram, a  formal  parameter  is  subject  to  any
  178. constraint   resulting   from   the   type  mark  given  in  its  parameter
  179. specification.  For a formal parameter of an unconstrained array type,  the
  180. bounds  are obtained from the actual parameter, and the formal parameter is
  181. constrained by these bounds (see 3.6.1).   For  a  formal  parameter  whose
  182. declaration  specifies  an  unconstrained  (private  or  record)  type with
  183. discriminants, the discriminants of the formal  parameter  are  initialized
  184. with the values of the corresponding discriminants of the actual parameter;
  185. the  formal parameter is unconstrained if and only if the mode is in out or
  186. out and the variable  name  given  for  the  actual  parameter  denotes  an
  187. unconstrained variable (see 3.7.1 and 6.4.1).
  188.  
  189.  
  190. If the actual parameter of a subprogram call is a subcomponent that depends
  191. on discriminants of an unconstrained record variable, then the execution of
  192. the  call  is  erroneous  if  the  value of any of the discriminants of the
  193. variable is changed by this execution;  this rule does  not  apply  if  the
  194. mode  is  in and the type of the subcomponent is a scalar type or an access
  195. type.
  196.  
  197. Notes:
  198.  
  199.  
  200. For parameters of array and record types, the parameter passing rules  have
  201. these consequences:
  202.  
  203.  
  204.   -  If the execution of a subprogram  is  abandoned  as  a  result  of  an
  205.      exception,  the  final value of an actual parameter of such a type can
  206.      be either its value before the call or a value assigned to the  formal
  207.      parameter during the execution of the subprogram.
  208.  
  209.  
  210.   -  If no actual parameter of such a type is accessible by more  than  one
  211.      path,  then  the effect of a subprogram call (unless abandoned) is the
  212.      same whether or not the  implementation  uses  copying  for  parameter
  213.      passing.   If,  however,  there  are  multiple  access paths to such a
  214.      parameter (for example,  if  a  global  variable,  or  another  formal
  215.      parameter, refers to the same actual parameter), then the value of the
  216.      formal  is  undefined after updating the actual other than by updating
  217.      the formal.  A program using such an undefined value is erroneous.
  218.  
  219.  
  220. The same parameter modes are defined for formal parameters of entries  (see
  221. 9.5)  with  the same meaning as for subprograms.  Different parameter modes
  222. are defined for generic formal parameters (see 12.1.1).
  223.  
  224.  
  225. For all modes, if an actual parameter designates  a  task,  the  associated
  226. formal   parameter  designates  the  same  task;   the  same  holds  for  a
  227. subcomponent of an actual parameter and the corresponding  subcomponent  of
  228. the associated formal parameter.
  229.  
  230.  
  231. References:   access  type  3.8,  actual  parameter  6.4.1, array type 3.6,
  232. assignment 5.2, bound of an  array  3.6.1,  constraint  3.3,  depend  on  a
  233. discriminant 3.7.1, discriminant 3.7.1, entry call statement 9.5, erroneous
  234. 1.6,  evaluation  4.5,  exception 11, expression 4.4, formal parameter 6.1,
  235. generic formal parameter 12.1, global 8.1, mode 6.1, null access value 3.8,
  236. object 3.2, parameter specification 6.1, private type 7.4, record type 3.7,
  237. scalar type 3.5, subcomponent 3.3, subprogram  body  6.3,  subprogram  call
  238. statement  6.4, task 9, task type 9.2, type mark 3.3.2, unconstrained array
  239. type  3.6,  unconstrained  type  with  discriminants  3.7.1,  unconstrained
  240. variable 3.2.1, variable 3.2.1
  241.  
  242. 6.3  Subprogram Bodies
  243.  
  244.  
  245. A subprogram body specifies the execution of a subprogram.
  246.  
  247.  
  248.     subprogram_body ::=
  249.         subprogram_specification is
  250.            [declarative_part]
  251.         begin
  252.             sequence_of_statements
  253.        [exception
  254.             exception_handler
  255.            {exception_handler}]
  256.         end [designator];      
  257.  
  258.  
  259. The  declaration  of  a  subprogram  is optional.  In the absence of such a
  260. declaration, the subprogram specification of the subprogram body  (or  body
  261. stub) acts as the declaration.  For each subprogram declaration, there must
  262. be  a  corresponding  body  (except  for  a  subprogram  written in another
  263. language, as explained in section 13.9).  If both a declaration and a  body
  264. are  given,  the  subprogram  specification of the body must conform to the
  265. subprogram  specification  of  the  declaration  (see  section  6.3.1   for
  266. conformance rules).
  267.  
  268.  
  269. If a designator appears at the end of a subprogram body, it must repeat the
  270. designator of the subprogram specification.
  271.  
  272.  
  273. The  elaboration of a subprogram body has no other effect than to establish
  274. that the body can from then on be used for the execution of  calls  of  the
  275. subprogram.
  276.  
  277.  
  278. The  execution  of  a  subprogram body is invoked by a subprogram call (see
  279. 6.4).  For this  execution,  after  establishing  the  association  between
  280. formal  parameters  and actual parameters, the declarative part of the body
  281. is elaborated, and the sequence of statements of the body is then executed.
  282. Upon completion of the body,   return  is  made  to  the  caller  (and  any
  283. necessary  copying  back  of formal to actual parameters occurs (see 6.2)).
  284. The optional exception handlers at the end  of  a  subprogram  body  handle
  285. exceptions raised during the execution of the sequence of statements of the
  286. subprogram body (see 11.4).
  287.  
  288. Note:
  289.  
  290.  
  291. It  follows  from  the  visibility rules that if a subprogram declared in a
  292. package is to be visible outside the package,  a  subprogram  specification
  293. must  be  given in the visible part of the package.  The same rules dictate
  294. that a subprogram declaration must be given if a  call  of  the  subprogram
  295. occurs  textually  before  the  subprogram  body (the declaration must then
  296. occur earlier than the call in the  program  text).   The  rules  given  in
  297. sections   3.9  and  7.1  imply  that  a  subprogram  declaration  and  the
  298. corresponding body must both occur immediately within the same  declarative
  299. region.
  300.  
  301.  
  302. Example of subprogram body:
  303.  
  304.     procedure PUSH(E : in ELEMENT_TYPE; S : in out STACK) is
  305.     begin
  306.        if S.INDEX = S.SIZE then
  307.           raise STACK_OVERFLOW;
  308.        else
  309.           S.INDEX := S.INDEX + 1;
  310.           S.SPACE(S.INDEX) := E;
  311.        end if;
  312.     end PUSH;
  313.  
  314.  
  315. References:   actual  parameter  6.4.1,  body  stub  10.2,  conform  6.3.1,
  316. declaration 3.1, declarative part 3.9, declarative region  8.1,  designator
  317. 6.1,  elaboration  3.9,  elaboration has no other effect 3.1, exception 11,
  318. exception handler 11.2, formal parameter 6.1, occur immediately within 8.1,
  319. package 7, sequence of statements 5.1, subprogram 6, subprogram  call  6.4,
  320. subprogram  declaration  6.1, subprogram specification 6.1, visibility 8.3,
  321. visible part 7.2
  322.  
  323. 6.3.1  Conformance Rules
  324.  
  325.  
  326. Whenever the language rules require or allow the specification of  a  given
  327. subprogram  to be provided in more than one place, the following variations
  328. are allowed at each place:
  329.  
  330.  
  331.   -  A numeric literal can be replaced by a different  numeric  literal  if
  332.      and only if both have the same value.
  333.  
  334.  
  335.   -  A simple name can be replaced by an expanded name in which this simple
  336.      name is the selector, if and only if at both places the meaning of the
  337.      simple name is given by the same declaration.
  338.  
  339.  
  340.   -  A string literal given as an operator symbol  can  be  replaced  by  a
  341.      different  string  literal  if  and  only  if  both represent the same
  342.      operator.
  343.  
  344.  
  345. Two subprogram specifications are said to conform if, apart  from  comments
  346. and  the  above  allowed  variations, both specifications are formed by the
  347. same sequence of lexical elements, and corresponding lexical  elements  are
  348. given the same meaning by the visibility and overloading rules.
  349.  
  350.  
  351. Conformance  is  likewise defined for formal parts, discriminant parts, and
  352. type marks (for deferred constants and for actual parameters that have  the
  353. form of a type conversion (see 6.4.1)).
  354.  
  355. Notes:
  356.  
  357.  
  358. A  simple  name can be replaced by an expanded name even if the simple name
  359. is itself the prefix of a selected component.   For  example,  Q.R  can  be
  360. replaced by P.Q.R if Q is declared immediately within P.
  361.  
  362.  
  363. The  following  specifications  do not conform since they are not formed by
  364. the same sequence of lexical elements:
  365.  
  366.     procedure P(X,Y : INTEGER)
  367.     procedure P(X : INTEGER; Y : INTEGER)
  368.     procedure P(X,Y : in INTEGER)
  369.  
  370.  
  371. References:   actual  parameter  6.4  6.4.1,  allow   1.6,   comment   2.7,
  372. declaration   3.1,   deferred   constant   7.4.3,  direct  visibility  8.3,
  373. discriminant part 3.7.1, expanded name  4.1.3,  formal  part  6.1,  lexical
  374. element  2, name 4.1, numeric literal 2.4, operator symbol 6.1, overloading
  375. 6.6 8.7, prefix 4.1, selected component 4.1.3, selector 4.1.3, simple  name
  376. 4.1, subprogram specification 6.1, type conversion 4.6, visibility 8.3
  377.  
  378. 6.3.2  Inline Expansion of Subprograms
  379.  
  380.  
  381. The  pragma  INLINE  is  used  to  indicate  that  inline  expansion of the
  382. subprogram body is desired for every call of each of the named subprograms.
  383. The form of this pragma is as follows:
  384.  
  385.  
  386.     pragma INLINE (name {, name});
  387.  
  388. Each name is either the name of a subprogram  or  the  name  of  a  generic
  389. subprogram.   The  pragma  INLINE  is  only  allowed  at  the  place  of  a
  390. declarative item in a declarative part or package specification, or after a
  391. library unit in a compilation, but before any subsequent compilation  unit.
  392.  
  393.  
  394. If  the  pragma  appears at the place of a declarative item, each name must
  395. denote a  subprogram  or  a  generic  subprogram  declared  by  an  earlier
  396. declarative item of the same declarative part or package specification.  If
  397. several  (overloaded)  subprograms  satisfy  this  requirement,  the pragma
  398. applies to all of them.  If the pragma appears after a given library  unit,
  399. the  only  name allowed is the name of this unit.  If the name of a generic
  400. subprogram is mentioned in the pragma, this indicates that inline expansion
  401. is desired for calls of all subprograms obtained by  instantiation  of  the
  402. named generic unit.
  403.  
  404.  
  405. The  meaning of a subprogram is not changed by the pragma INLINE.  For each
  406. call of the named subprograms, an implementation is free to  follow  or  to
  407. ignore  the  recommendation expressed by the pragma.  (Note, in particular,
  408. that the recommendation  cannot  generally  be  followed  for  a  recursive
  409. subprogram.)
  410.  
  411.  
  412. References:    allow   1.6,   compilation   10.1,  compilation  unit  10.1,
  413. declarative item  3.9,  declarative  part  3.9,  generic  subprogram  12.1,
  414. generic  unit  12  12.1,  instantiation  12.3, library unit 10.1, name 4.1,
  415. overloading 6.6 8.7, package specification 7.1, pragma 2.8,  subprogram  6,
  416. subprogram body 6.3, subprogram call 6.4
  417.  
  418. 6.4  Subprogram Calls
  419.  
  420.  
  421. A  subprogram call is either a procedure call statement or a function call;
  422. it invokes the execution of the corresponding subprogram  body.   The  call
  423. specifies  the  association  of  the actual parameters, if any, with formal
  424. parameters of the subprogram.
  425.  
  426.  
  427.     procedure_call_statement ::=
  428.         procedure_name [actual_parameter_part];
  429.  
  430.     function_call ::=
  431.         function_name [actual_parameter_part]
  432.  
  433.     actual_parameter_part ::=
  434.         (parameter_association {, parameter_association})
  435.  
  436.     parameter_association ::=
  437.        [formal_parameter =>] actual_parameter
  438.  
  439.     formal_parameter ::= parameter_simple_name
  440.  
  441.     actual_parameter ::=
  442.        expression | variable_name | type_mark(variable_name)
  443.  
  444.  
  445. Each  parameter  association  associates  an  actual   parameter   with   a
  446. corresponding  formal  parameter.   A  parameter  association is said to be
  447. named if the formal parameter is named explicitly;  it is otherwise said to
  448. be  positional.   For  a  positional  association,  the  actual   parameter
  449. corresponds  to  the  formal parameter with the same position in the formal
  450. part.
  451.  
  452.  
  453. Named associations can be given in any order, but if  both  positional  and
  454. named  associations are used in the same call, positional associations must
  455. occur first, at their normal position.  Hence once a named  association  is
  456. used, the rest of the call must use only named associations.
  457.  
  458.  
  459. For  each  formal parameter of a subprogram, a subprogram call must specify
  460. exactly one corresponding  actual  parameter.   This  actual  parameter  is
  461. specified either explicitly, by a parameter association, or, in the absence
  462. of such an association, by a default expression (see 6.4.2).
  463.  
  464.  
  465. The parameter associations of a subprogram call are evaluated in some order
  466. that  is not defined by the language.  Similarly, the language rules do not
  467. define in which order the values of in out or  out  parameters  are  copied
  468. back into the corresponding actual parameters (when this is done).
  469.  
  470.  
  471. Examples of procedure calls:
  472.  
  473.     TRAVERSE_TREE;                                                 --  see 6.1
  474.     TABLE_MANAGER.INSERT(E);                                       --  see 7.5
  475.     PRINT_HEADER(128, TITLE, TRUE);                                --  see 6.1
  476.  
  477.     SWITCH(FROM => X, TO => NEXT);                                 --  see 6.1
  478.     PRINT_HEADER(128, HEADER => TITLE, CENTER => TRUE);            --  see 6.1
  479.     PRINT_HEADER(HEADER => TITLE, CENTER => TRUE, PAGES => 128);   --  see 6.1
  480.  
  481.  
  482. Examples of function calls:
  483.  
  484.     DOT_PRODUCT(U, V)   --  see 6.1 and 6.5
  485.     CLOCK               --  see 9.6
  486.  
  487.  
  488. References:   default expression for a formal parameter 6.1, erroneous 1.6,
  489. expression 4.4, formal parameter 6.1, formal part  6.1,  name  4.1,  simple
  490. name 4.1, subprogram 6, type mark 3.3.2, variable 3.2.1
  491.  
  492. 6.4.1  Parameter Associations
  493.  
  494.  
  495. Each  actual  parameter must have the same type as the corresponding formal
  496. parameter.
  497.  
  498.  
  499. An actual parameter associated with a formal parameter of mode in  must  be
  500. an expression;  it is evaluated before the call.
  501.  
  502.  
  503. An  actual  parameter  associated with a formal parameter of mode in out or
  504. out must be either the name of a  variable,  or  of  the  form  of  a  type
  505. conversion  whose  argument is the name of a variable.  In either case, for
  506. the mode in out, the variable must not be a formal parameter of mode out or
  507. a subcomponent thereof.  For an actual parameter that has  the  form  of  a
  508. type conversion, the type mark must conform (see 6.3.1) to the type mark of
  509. the formal parameter;  the allowed operand and target types are the same as
  510. for type conversions (see 4.6).
  511.  
  512.  
  513. The  variable  name  given for an actual parameter of mode in out or out is
  514. evaluated before the call.  If the actual parameter has the form of a  type
  515. conversion,  then  before  the  call,  for  a parameter of mode in out, the
  516. variable is converted to the specified type;  after (normal) completion  of
  517. the  subprogram  body,  for  a  parameter of mode in out or out, the formal
  518. parameter is converted back  to  the  type  of  the  variable.   (The  type
  519. specified in the conversion must be that of the formal parameter.)
  520.  
  521.  
  522. The  following constraint checks are performed for parameters of scalar and
  523. access types:
  524.  
  525.  
  526.   -  Before the call:  for a parameter of mode in or in out, it is  checked
  527.      that  the  value of the actual parameter belongs to the subtype of the
  528.      formal parameter.
  529.  
  530.  
  531.   -  After (normal) completion of the subprogram body:  for a parameter  of
  532.      mode  in  out  or  out,  it  is  checked  that the value of the formal
  533.      parameter belongs to the subtype of the actual variable.  In the  case
  534.      of  a  type conversion, the value of the formal parameter is converted
  535.      back and the check applies to the result of the conversion.
  536.  
  537.  
  538. In each of the above cases, the execution of the program  is  erroneous  if
  539. the checked value is undefined.
  540.  
  541.  
  542. For  other  types,  for  all  modes, a check is made before the call as for
  543. scalar and access types;  no check is made upon return.
  544.  
  545.  
  546. The exception CONSTRAINT_ERROR is raised at the  place  of  the  subprogram
  547. call if either of these checks fails.
  548.  
  549. Note:
  550.  
  551.  
  552. For array types and for types with discriminants, the check before the call
  553. is  sufficient (a check upon return would be redundant) if the type mark of
  554. the formal parameter denotes a constrained  subtype,  since  neither  array
  555. bounds nor discriminants can then vary.
  556.  
  557.  
  558. If this type mark denotes an unconstrained array type, the formal parameter
  559. is constrained with the bounds of the corresponding actual parameter and no
  560. check  (neither  before  the  call  nor upon return) is needed (see 3.6.1).
  561. Similarly, no check is needed if the type  mark  denotes  an  unconstrained
  562. type  with  discriminants,  since  the formal parameter is then constrained
  563. exactly as the corresponding actual parameter (see 3.7.1).
  564.  
  565.  
  566. References:  actual parameter 6.4, array bound 3.6, array type 3.6, call of
  567. a subprogram 6.4, conform 6.3.1, constrained subtype 3.3,  constraint  3.3,
  568. constraint_error   exception   11.1,  discriminant  3.7.1,  erroneous  1.6,
  569. evaluation 4.5, evaluation of a name 4.1, expression 4.4, formal  parameter
  570. 6.1,  mode 6.1, name 4.1, parameter association 6.4, subtype 3.3, type 3.3,
  571. type conversion  4.6,  type  mark  3.3.2,  unconstrained  array  type  3.6,
  572. unconstrained   type  with  discriminants  3.7.1,  undefined  value  3.2.1,
  573. variable 3.2.1
  574.  
  575. 6.4.2  Default Parameters
  576.  
  577.  
  578. If a parameter specification includes a default expression for a  parameter
  579. of  mode  in,  then  corresponding  subprogram  calls  need  not  include a
  580. parameter association for the parameter.  If  a  parameter  association  is
  581. thus  omitted from a call, then the rest of the call, following any initial
  582. positional associations, must use only named associations.
  583.  
  584.  
  585. For any omitted parameter association, the default expression is  evaluated
  586. before  the  call  and  the  resulting  value is used as an implicit actual
  587. parameter.
  588.  
  589.  
  590. Examples of procedures with default values:
  591.  
  592.     procedure ACTIVATE(PROCESS : in PROCESS_NAME;
  593.                        AFTER   : in PROCESS_NAME := NO_PROCESS;
  594.                        WAIT    : in DURATION := 0.0;
  595.                        PRIOR   : in BOOLEAN := FALSE);
  596.  
  597.     procedure PAIR(LEFT, RIGHT : PERSON_NAME := new PERSON);
  598.  
  599.  
  600. Examples of their calls:
  601.  
  602.     ACTIVATE(X);
  603.     ACTIVATE(X, AFTER => Y);
  604.     ACTIVATE(X, WAIT => 60.0, PRIOR => TRUE);
  605.     ACTIVATE(X, Y, 10.0, FALSE);
  606.  
  607.     PAIR;
  608.     PAIR(LEFT => new PERSON, RIGHT => new PERSON);
  609.  
  610. Note:
  611.  
  612.  
  613. If a default expression is used for two or more parameters  in  a  multiple
  614. parameter  specification, the default expression is evaluated once for each
  615. omitted parameter.  Hence in the above examples, the two calls of PAIR  are
  616. equivalent.
  617.  
  618.  
  619. References:   actual  parameter  6.4.1,  default  expression  for  a formal
  620. parameter 6.1, evaluation  4.5,  formal  parameter  6.1,  mode  6.1,  named
  621. parameter   association   6.4,   parameter   association   6.4,   parameter
  622. specification 6.1, positional parameter association  6.4,  subprogram  call
  623. 6.4
  624.  
  625. 6.5  Function Subprograms
  626.  
  627.  
  628. A function is a subprogram that returns a value (the result of the function
  629. call).   The  specification  of  a  function  starts with the reserved word
  630. function, and the parameters, if any, must have the mode in  (whether  this
  631. mode  is  specified  explicitly  or  implicitly).   The  statements  of the
  632. function body (excluding statements of program units that are inner to  the
  633. function  body)  must  include one or more return statements specifying the
  634. returned value.
  635.  
  636.  
  637. The exception PROGRAM_ERROR is raised if a function body is left  otherwise
  638. than  by  a  return statement.  This does not apply if the execution of the
  639. function is abandoned as a result of an exception.
  640.  
  641.  
  642. Example:
  643.  
  644.     function DOT_PRODUCT(LEFT, RIGHT : VECTOR) return REAL is
  645.        SUM : REAL := 0.0;
  646.     begin
  647.        CHECK(LEFT'FIRST = RIGHT'FIRST and LEFT'LAST = RIGHT'LAST);
  648.        for J in LEFT'RANGE loop
  649.           SUM := SUM + LEFT(J)*RIGHT(J);
  650.        end loop;
  651.        return SUM;
  652.     end DOT_PRODUCT;
  653.  
  654.  
  655. References:  exception 11, formal parameter  6.1,  function  6.1,  function
  656. body  6.3,  function  call  6.4,  function  specification  6.1,  mode  6.1,
  657. program_error exception 11.1, raising of exceptions  11,  return  statement
  658. 5.8, statement 5
  659.  
  660. 6.6  Parameter and Result Type Profile - Overloading of Subprograms
  661.  
  662.  
  663. Two  formal  parts  are said to have the same parameter type profile if and
  664. only if they have the same number of  parameters,  and  at  each  parameter
  665. position corresponding parameters have the same base type.  A subprogram or
  666. entry  has the same parameter and result type profile as another subprogram
  667. or entry if and only if both have the  same  parameter  type  profile,  and
  668. either both are functions with the same result base type, or neither of the
  669. two is a function.
  670.  
  671.  
  672. The  same  subprogram  identifier or operator symbol can be used in several
  673. subprogram specifications.  The identifier or operator symbol is then  said
  674. to  be  overloaded;   the subprograms that have this identifier or operator
  675. symbol are also said to be overloaded  and  to  overload  each  other.   As
  676. explained  in  section  8.3, if two subprograms overload each other, one of
  677. them can hide the other only if both subprograms have  the  same  parameter
  678. and  result  type  profile (see section 8.3 for the other requirements that
  679. must be met for hiding).
  680.  
  681.  
  682. A call to an overloaded subprogram is ambiguous (and therefore illegal)  if
  683. the  name  of  the  subprogram,  the  number of parameter associations, the
  684. types and the order of the actual  parameters,  the  names  of  the  formal
  685. parameters  (if  named  associations  are  used),  and the result type (for
  686. functions)  are  not  sufficient  to  determine  exactly  one  (overloaded)
  687. subprogram specification.
  688.  
  689.  
  690. Examples of overloaded subprograms:
  691.  
  692.     procedure PUT(X : INTEGER);
  693.     procedure PUT(X : STRING);
  694.  
  695.     procedure SET(TINT   : COLOR);
  696.     procedure SET(SIGNAL : LIGHT);
  697.  
  698.  
  699. Examples of calls:
  700.  
  701.     PUT(28);
  702.     PUT("no possible ambiguity here");
  703.  
  704.     SET(TINT   => RED);
  705.     SET(SIGNAL => RED);
  706.     SET(COLOR'(RED));
  707.  
  708.     --  SET(RED) would be ambiguous since RED may
  709.     --  denote a value either of type COLOR or of type LIGHT
  710.  
  711. Notes:
  712.  
  713.  
  714. The  notion of parameter and result type profile does not include parameter
  715. names, parameter modes, parameter subtypes, default expressions  and  their
  716. presence or absence.
  717.  
  718.  
  719. Ambiguities  may (but need not) arise when actual parameters of the call of
  720. an  overloaded  subprogram  are  themselves  overloaded   function   calls,
  721. literals,  or  aggregates.  Ambiguities may  also (but need not) arise when
  722. several overloaded subprograms belonging to different packages are visible.
  723. These ambiguities can usually  be  resolved  in  several  ways:   qualified
  724. expressions  can  be  used  for  some or all actual parameters, and for the
  725. result, if  any;   the  name  of  the  subprogram  can  be  expressed  more
  726. explicitly as an expanded name;  finally, the subprogram can be renamed.
  727.  
  728.  
  729. References:   actual parameter 6.4.1, aggregate 4.3, base type 3.3, default
  730. expression for a formal parameter 6.1, entry  9.5,  formal  parameter  6.1,
  731. function  6.5,  function call 6.4, hiding 8.3, identifier 2.3, illegal 1.6,
  732. literal 4.2, mode 6.1, named parameter  association  6.4,  operator  symbol
  733. 6.1,  overloading  8.7, package 7, parameter of a subprogram 6.2, qualified
  734. expression 4.7, renaming declaration 8.5, result subtype 6.1, subprogram 6,
  735. subprogram specification 6.1, subtype 3.3, type 3.3
  736.  
  737. 6.7  Overloading of Operators
  738.  
  739.  
  740. The declaration of a function whose designator is  an  operator  symbol  is
  741. used  to  overload an operator.  The sequence of characters of the operator
  742. symbol must be either a logical, a relational, a  binary  adding,  a  unary
  743. adding, a multiplying, or a highest precedence operator (see 4.5).  Neither
  744. membership  tests  nor  the  short-circuit  control  forms  are  allowed as
  745. function designators.
  746.  
  747.  
  748. The subprogram specification  of  a  unary  operator  must  have  a  single
  749. parameter.  The subprogram specification of a binary operator must have two
  750. parameters;   for  each use of this operator, the first parameter takes the
  751. left operand as actual parameter, the  second  parameter  takes  the  right
  752. operand.   Similarly,  a generic function instantiation whose designator is
  753. an operator symbol is only allowed if  the  specification  of  the  generic
  754. function  has  the corresponding number of parameters.  Default expressions
  755. are not allowed for the parameters of an operator (whether the operator  is
  756. declared  with  an  explicit  subprogram  specification  or  by  a  generic
  757. instantiation).
  758.  
  759.  
  760. For each of the operators "+" and "-", overloading is  allowed  both  as  a
  761. unary and as a binary operator.
  762.  
  763.  
  764. The explicit declaration of a function that overloads the equality operator
  765. "=",  other  than  by  a  renaming  declaration,  is  only  allowed if both
  766. parameters are of the same limited type.  An overloading of  equality  must
  767. deliver  a  result  of  the  predefined  type  BOOLEAN;  it also implicitly
  768. overloads the inequality  operator  "/="  so  that  this  still  gives  the
  769. complementary result to the equality operator.  Explicit overloading of the
  770. inequality operator is not allowed.
  771.  
  772.  
  773. A  renaming  declaration  whose designator is the equality operator is only
  774. allowed to rename another equality operator.  (For example, such a renaming
  775. declaration can be used when equality  is  visible  by  selection  but  not
  776. directly visible.)
  777.  
  778. Note:
  779.  
  780.  
  781. Overloading  of relational operators does not affect basic comparisons such
  782. as testing for membership in a range or the choices in a case statement.
  783.  
  784.  
  785. Examples:
  786.  
  787.     function "+" (LEFT, RIGHT : MATRIX) return MATRIX;
  788.     function "+" (LEFT, RIGHT : VECTOR) return VECTOR;
  789.  
  790.     --  assuming that A, B, and C are of the type VECTOR
  791.     --  the three following assignments are equivalent
  792.  
  793.     A := B + C;
  794.  
  795.     A := "+"(B, C);
  796.     A := "+"(LEFT => B, RIGHT => C);
  797.  
  798.  
  799. References:  allow 1.6, actual parameter 6.4.1, binary adding operator  4.5
  800. 4.5.3,  boolean  predefined type 3.5.3, character 2.1, complementary result
  801. 4.5.2, declaration 3.1, default expression  for  a  formal  parameter  6.1,
  802. designator  6.1,  directly  visible  8.3,  equality  operator  4.5,  formal
  803. parameter 6.1, function declaration 6.1, highest  precedence  operator  4.5
  804. 4.5.6,  implicit  declaration  3.1, inequality operator 4.5.2, limited type
  805. 7.4.4, logical operator 4.5 4.5.1, membership test 4.5  4.5.2,  multiplying
  806. operator 4.5 4.5.5, operator 4.5, operator symbol 6.1, overloading 6.6 8.7,
  807. relational  operator  4.5 4.5.2, short-circuit control form 4.5 4.5.1, type
  808. definition 3.3.1, unary adding operator 4.5 4.5.4, visible by selection 8.3
  809.